import { h, ref } from 'vue';
import { NTag } from 'naive-ui';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';

export class State {
  public code = ''; // 代码
  public name = ''; // name
  public lastPrice = ''; // 最新价
  public changePercent = ''; // 涨跌幅
  public changeAmount = ''; // 涨跌额
  public volume = ''; // 成交量
  public turnover = ''; // 成交额
  public amplitude = ''; // 振幅
  public high = ''; // 最高
  public low = ''; // 最低
  public open = ''; // 今开
  public closed = ''; // 昨收
  public volumeRatio = ''; // 量比
  public turnoverRate = ''; // 换手率
  public peRatio = ''; // 动态市盈率
  public pbRatio = ''; // 市净率
  public marketCap = ''; // 总市值
  public circulatingMarketCap = ''; // 流通市值
  public riseSpeed = ''; // 涨速
  public change5Min = ''; // 5分钟涨跌
  public changeErcent60Day = ''; // 60日涨跌幅
  public ytdChangePercent = ''; // 年初至今涨跌幅
  public date = ''; // 日期

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

export const rules = {};

export const schemas = ref<FormSchema[]>([
  {
    field: 'code',
    component: 'NInput',
    label: '代码',
    componentProps: {
      placeholder: '请输入代码',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

export const columns = [
  // {
  //   title: '日期',
  //   key: 'date',
  // },
  {
    title: '代码',
    key: 'code',
  },
  {
    title: '名字',
    key: 'name',
  },
  {
    title: '最新价',
    key: 'lastPrice',
  },
  {
    title: '涨跌幅',
    key: 'changePercent',

    render: (row: any) => {
      return h(
        NTag,
        {
          type: row.changePercent < 0 ? 'success' : 'error',
        },
        {
          default: () => row.changePercent + '%',
        }
      );
    },
  },
  {
    title: '涨跌额',
    key: 'changeAmount',

    render: (row: any) => {
      return h(
        NTag,
        {
          type: row.changeAmount < 0 ? 'success' : 'error',
        },
        {
          default: () => row.changeAmount,
        }
      );
    },
  },
  {
    title: '成交额',
    key: 'turnover',

    render: (row: any) => {
      const num = Number(row.turnover);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        return (num / (10000 * 10000)).toFixed(2) + '亿';
      }
    },
  },
  {
    title: '振幅',
    key: 'amplitude',

    render: (row: any) => {
      const num = Number(row.amplitude);
      return num + '%';
    },
  },
  {
    title: '最高',
    key: 'high',
  },
  {
    title: '最低',
    key: 'low',
  },
  {
    title: '今开',
    key: 'open',
  },
  {
    title: '昨收',
    key: 'closed',
  },
  {
    title: '量比',
    key: 'volumeRatio',
  },
  {
    title: '换手率',
    key: 'turnoverRate',

    render: (row: any) => {
      const num = Number(row.turnoverRate);
      return num + '%';
    },
  }, // {
  //   title: '动态市盈率',
  //   key: 'peRatio',
  // },
  // {
  //   title: '市净率',
  //   key: 'pbRatio',
  // },
  {
    title: '总市值',
    key: 'marketCap',

    render: (row: any) => {
      const num = Number(row.marketCap);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        return (num / (10000 * 10000)).toFixed(2) + '亿';
      }
    },
  },
  {
    title: '流通市值',
    key: 'circulatingMarketCap',

    render: (row: any) => {
      const num = Number(row.circulatingMarketCap);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        return (num / (10000 * 10000)).toFixed(2) + '亿';
      }
    },
  }, // {
  {
    title: '流入',
    key: 'inflow',

    render: (row: any) => {
      const num = Number(row.inflow);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        return (num / (10000 * 10000)).toFixed(2) + '亿';
      }
    },
  },
  {
    title: '流出',
    key: 'outflow',

    render: (row: any) => {
      const num = Number(row.outflow);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        return (num / (10000 * 10000)).toFixed(2) + '亿';
      }
    },
  },
  {
    title: '净流入',
    key: 'netInflow',

    render: (row: any) => {
      const num = Number(row.netInflow);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        return (num / (10000 * 10000)).toFixed(2) + '亿';
      }
    },
  },
  {
    title: '60日涨幅',
    key: 'changeErcent60Day',
    render: (row: any) => {
      return h(
        NTag,
        {
          type: row.changeErcent60Day < 0 ? 'success' : 'error',
        },
        {
          default: () => row.changeErcent60Day + '%',
        }
      );
    },
  },
  {
    title: '今年涨幅',
    key: 'ytdChangePercent',

    render: (row: any) => {
      return h(
        NTag,
        {
          type: row.ytdChangePercent < 0 ? 'success' : 'error',
        },
        {
          default: () => row.ytdChangePercent + '%',
        }
      );
    },
  },
];
